package test.marui.com.mytest.view.photoOperation;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;

import java.io.File;


/**
 * Author： by MR on 2017/8/25.
 */

public class ImgUtils {

    public static Uri createImageUri(Context context) {

        String state = Environment.getExternalStorageState();
        File rootDir = state.equals(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory() : context.getCacheDir();

        //创建文件
        File folderDir = getImgFile(rootDir);

        if (!folderDir.exists() && folderDir.mkdirs()) {

        }

        File tmpFile;
        String fileName = System.currentTimeMillis() + ".JPEG";
        tmpFile = new File(folderDir, fileName);

        // 调用parUri方法转成uri
        return parUri(context, tmpFile);
    }

    /**
     * 生成uri
     *
     * @param cameraFile
     * @return
     */
    public static Uri parUri(Context context, File cameraFile) {
        Uri imageUri;
        String authority = context.getPackageName() + ".provider";
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            //通过FileProvider创建一个content类型的Uri
            imageUri = FileProvider.getUriForFile(context, authority, cameraFile);
        } else {
            imageUri = Uri.fromFile(cameraFile);
        }
        return imageUri;
    }

    private static File getImgFile(File rootDir) {
        File folderDir = new File(rootDir.getAbsolutePath() + "/pm/picture/");
        return folderDir;
    }

    public static File getExcelFile(File rootDir) {
        File folderDir = new File(rootDir.getAbsolutePath() + "/pm/excel/");
        return folderDir;
    }

}
